<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use addons\third\model\Third;
use think\Config;
use app\common\library\Auth;
use fast\Http;
use addons\third\library\Service;
/**
 * 个人中心接口
 */
class My extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        $this->token = $this->request->post('token');
        if ($this->request->action() == 'login' && $this->token) {
            $this->request->post(['token' => '']);
        }
        parent::_initialize();
    }  


         /**
     * 会员登录
     * @ApiSummary  会员登录
     * @ApiMethod   (POST)
     * @ApiParams   (name="username", type="string", required=true, description="账号")
     * @ApiParams   (name="password", type="string", required=true, description="密码")
     * @ApiReturn   ({
         {
          "code": 1,
          "msg": "登录成功",
          "time": "1586832433",
          "data": {
            "userinfo": {
              "id": 1,
              "username": "admin",
              "nickname": "admin",
              "mobile": "13888888888",
              "avatar": "/assets/img/qrcode.png",
              "score": 0,
              "token": "ec723bcd-41d6-4147-ab2d-6434b9c380ad",
              "user_id": 1,
              "createtime": 1586832434,
              "expiretime": 1589424434,
              "expires_in": 2592000
            }
          }
        }
      })
     */
    
    public function login()
    {
        $username = $this->request->request('username');
        $password = $this->request->request('password');
        if (!$username || !$password) {
            $this->error(__('Invalid parameters'));
        }
        $ret = $this->auth->login($username, $password);
        if ($ret) {
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success(__('Logged in successful'), $data);
        } else {
            $this->error($this->auth->getError());
        }
    }

     /**
    * 获取个人信息 
    **/
    public function userInfo(){
        $user = $this->auth->getUser();
        $this->success('获取个人信息',$user->toArray());
    }

     


}
